/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.trandio.manager;

import java.io.*;
import java.net.URLDecoder;
import java.net.URLEncoder;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author root
 */
@WebServlet("/DownloadFile")
public class DownloadFile extends HttpServlet {

    private static final long serialVersionUID = 11;
    final String FILE_LOCATION = "/opt/rips/";
    final String[][] contentTypes = {{"xml", "text/xml"}, {"pdf", "application/pdf"}, {"zip", "application/zip"}};

    /**
     * Processes requests for both HTTP
     * <code>GET</code> and
     * <code>POST</code> methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     *
     *
     */
    private String getContentType(String fileType) {
        String returnType = null;
        for (int i = 0; i < contentTypes.length; i++) {
            if (fileType.equals(contentTypes[i][0])) {
                returnType = contentTypes[i][1];
                break;
            }

        }

        return returnType;
    }

    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        try {
            String nf =  URLDecoder.decode(request.getParameter("fileName")) ;
            Object requestObject = request.getParameter("fileName");
            if (nf != null) {
                String fileName = nf;
                String contentType = getContentType(fileName.split("\\.")[1]);
                response.setContentType(contentType);
                File file = new File(FILE_LOCATION + "/" + fileName);
                response.addHeader("Content-Disposition", "attachment; filename="+URLEncoder.encode(fileName));
                response.setContentLength((int) file.length());
                ServletOutputStream servletOutputStream = response.getOutputStream();
                BufferedInputStream bufferedInputStream = new BufferedInputStream(new FileInputStream(file));
                int byteRead = bufferedInputStream.read();
                while (byteRead != -1) {
                    servletOutputStream.write(byteRead);
                    byteRead = bufferedInputStream.read();
                }
                if (servletOutputStream != null) {
                    servletOutputStream.close();
                }
                if (bufferedInputStream != null) {
                    bufferedInputStream.close();
                }
            }

        } finally {
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
